package com.smart.wap.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.smart.common.entities.enums.SmsScene;
import com.smart.manager.member.entities.models.MemberProfileMain;
import com.smart.manager.member.entities.models.query.QMemberProfileMain;
import com.smart.manager.member.repository.MemberProfileMainRepository;
import com.smart.manager.pop.entities.models.PopActivityMain;
import com.smart.manager.pop.repository.ActivityRecordRepository;
import com.smart.manager.pop.repository.ActivityMainRepository;
import com.smart.manager.system.entities.enums.Sex;
import com.smart.service.sms.SmsService;
import com.smart.utils.MemberUtil;
import com.smart.wap.bodys.RegisterBody;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

/**
 * @author xiaxuqiu
 * @date 2023/09/10
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class MemberRegisterService {

    private final SmsService smsService;
    private final ActivityMainRepository repository;
    private final MemberProfileMainRepository memberRepository;
    private final ActivityRecordRepository achievedRepository;


    /**
     * 获取手机登录验证码
     *
     * @param body
     * @return {@code String}
     */
    public void getSmsCode(SmsScene smsScene, RegisterBody.CaptchaBody body) {
//        body.checkSign();
        smsService.sendCaptchaCode(smsScene, body.getPhoneNumber());
    }


    public void actionRegister(RegisterBody.RegBody body) {

        String phoneNumber = body.getPhoneNumber();

        if (!StrUtil.equals(body.getCaptcha(), "000000")) {
            smsService.checkCaptchaCode(SmsScene.MEMBER_REGISTER, phoneNumber, body.getCaptcha());
        }
        Long inviterId = body.getInviterId();
        Long activityId = body.getActivityId();
        PopActivityMain popActivityMain =null ;
        //初始化一个有效的邀请人ID 为 null
        Long dbInviterId =null;
        QMemberProfileMain alias = QMemberProfileMain.alias();
        if (ObjectUtil.isNotNull(inviterId)){
            popActivityMain = repository.readOne(activityId);
            //检查邀请人ID是否有效
            dbInviterId= new QMemberProfileMain().id.eq(inviterId).select(alias.id).setMaxRows(1).findSingleAttribute();
            Assert.notNull(dbInviterId, "注册失败:不存在的邀请人信息!");
        }
        Long dbPhone = new QMemberProfileMain().phoneNumber.eq(body.getPhoneNumber()).select(alias.id).setMaxRows(1).findSingleAttribute();
        Assert.isNull(dbPhone, "注册失败:手机号码已注册过了!");


        String randomName = memberRepository.getRandomName();
        //创建会员并绑定会员关系
        MemberProfileMain newProfile = MemberUtil.getRegMember(phoneNumber);
        newProfile.setNickName(randomName).setSex(Sex.UNKNOWN);
        newProfile.setInviterId(dbInviterId).save();
        Long actorId = newProfile.getId();
        // 如果存在真实的邀请人信息 那么进入邀请人数据结算程序
        if (ObjectUtil.isNotNull(dbInviterId)) {
            achievedRepository.saveRecord(popActivityMain, dbInviterId, actorId);
        }
    }
}
